/**
 * 
 */
package com.hjx.service.system.impl;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.flex.remoting.RemotingDestination;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hjx.dao.MenuMapper;
import com.hjx.dao.RoleMenuMapper;
import com.hjx.model.Menu;
import com.hjx.model.MenuCriteria;
import com.hjx.model.MenuCriteria.Criteria;
import com.hjx.model.RoleMenu;
import com.hjx.model.RoleMenuCriteria;
import com.hjx.model.UserRole;
import com.hjx.model.UserRoleCriteria;
import com.hjx.service.impl.AbstractGenericService;
import com.hjx.service.system.IMenuService;

/**
 * @author huangjixin
 * 
 */
@Service(value = "menuService")
@RemotingDestination(value = "menuService",channels= {"my-amf","my-secure-amf"})
@Transactional
public class MenuServiceImpl extends AbstractGenericService<Menu, Integer>
		implements IMenuService {
	@Autowired
	private MenuMapper menuDao;
	@Autowired
	private RoleMenuMapper roleMenuMapper;
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.hjx.service.IGenericService#deleteByPrimaryKey(java.io.Serializable)
	 */
	@Override
	public int deleteByPrimaryKey(Integer id) {
		//-- 先删除子表数据
		RoleMenuCriteria roleMenuCriteria = new RoleMenuCriteria();
		roleMenuCriteria.createCriteria().andMenuIdEqualTo(id);
		List<RoleMenu> list0 = roleMenuMapper.selectByExample(roleMenuCriteria);
		for (RoleMenu roleMenu : list0) {
			roleMenuMapper.deleteByPrimaryKey(roleMenu.getId());
		}
		
		List<Menu>list = getByParentId(id);
		for (Menu menu : list) {
			deleteByPrimaryKey(menu.getId());
		}
		
		int i = menuDao.deleteByPrimaryKey(id);
		return i;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.hjx.service.IGenericService#insert(java.io.Serializable)
	 */
	@Override
	public int insert(Menu record) {
		super.insert(record);
	
		return menuDao.insert(record);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.hjx.service.IGenericService#insertSelective(java.io.Serializable)
	 */
	@Override
	public int insertSelective(Menu record) {
		super.insertSelective(record);
		
		return menuDao.insertSelective(record);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.hjx.service.IGenericService#selectAll()
	 */
	@Override
	public List<Menu> selectAll() {
		
		return menuDao.selectByExample(new MenuCriteria());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.hjx.service.IGenericService#selectByPrimaryKey(java.io.Serializable)
	 */
	@Override
	public Menu selectByPrimaryKey(Integer id) {
		return menuDao.selectByPrimaryKey(id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.hjx.service.IGenericService#updateByPrimaryKeySelective(java.io.
	 * Serializable)
	 */
	@Override
	public int updateByPrimaryKeySelective(Menu record) {
		super.updateByPrimaryKeySelective(record);
		return menuDao.updateByPrimaryKeySelective(record);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.hjx.service.IGenericService#updateByPrimaryKey(java.io.Serializable)
	 */
	@Override
	public int updateByPrimaryKey(Menu record) {
		if(record.getId() == 0){
			record.setId(null);
		}
		if(record.getParentid() == 0){
			record.setParentid(null);
		}
		
		return menuDao.updateByPrimaryKey(record);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.hjx.service.system.IMenuService#getMenu(java.lang.Integer)
	 */
	@Override
	public List<JSONObject> getMenu(Integer userId) {
		List<Menu> menus = selectByUserId(userId);
		List<Integer> ids = new ArrayList<Integer>();
		for (Menu menu : menus) {
			ids.add(menu.getId());
		}
		MenuCriteria menuCriteria = new MenuCriteria();
		Criteria criteria = menuCriteria.createCriteria();
		criteria.andParentidIsNull();
//		criteria.andIdIn(ids);

		List<Menu> list = menuDao.selectByExample(menuCriteria);
		List<JSONObject> children = new ArrayList<JSONObject>();
		for (Menu object : list) {
			JSONObject jsonObject = searialTcatgory(object);
			children.add(jsonObject);
		}
		return children;
	}

	private JSONObject searialTcatgory(Menu menu) {
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("id", menu.getId());
		jsonObject.put("parentid", menu.getParentid());
		jsonObject.put("name", menu.getName());
		jsonObject.put("path", menu.getPath());

		List<JSONObject> list = searialChild(menu);
		jsonObject.put("children", list);
		return jsonObject;
	}

	private List<JSONObject> searialChild(Menu menu) {
		List<JSONObject> children = null;
		List<Menu> list = getByParentId(menu.getId());
		if (list != null && list.size() > 0) {
			children = new ArrayList<JSONObject>();
		}
		for (Menu object : list) {
			JSONObject jsonObject = searialTcatgory(object);
			children.add(jsonObject);
		}

		return children;
	}

	/*
	 * 根据父类Id获得列表。
	 * 
	 * @see com.hjx.service.system.IMenuService#getByParentId(java.lang.Integer)
	 */
	@Override
	public List<Menu> getByParentId(Integer id) {
		MenuCriteria menuCriteria = new MenuCriteria();
		menuCriteria.createCriteria().andParentidEqualTo(id);
		return menuDao.selectByExample(menuCriteria);
	}

	@Override
	public List<Menu> selectByUserId(Integer userId) {
		List<Menu> list = menuDao.selectByUserId(userId);
		return list;
	}
}
